
package com.contactmodule.servlet;
import com.companymodule.bean.TbCompany;
import com.companymodule.bean.TbCompanyDepartMiddle;
import com.companymodule.dao.TbCompanyDAO;
import com.companymodule.dao.TbCompanyDepartMiddleDAO;
import com.contactmodule.bean.TbDepart;
import com.contactmodule.dao.TbContactDAO;
import com.contactmodule.dao.TbDepartDAO;
import com.usersmodule.bean.TbUsers;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbDepartServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        int userId=((TbUsers)request.getSession().getAttribute("Users")).getUserId();
        if("add".equals(method)){
        	 String departName=request.getParameter("departName");
        	 String departMemo=request.getParameter("departMemo");
       	 	 String parDepartId = request.getParameter("parDepartId");
        	 String lastParentId = parDepartId.substring(1);
        	 lastParentId = lastParentId.substring(0, lastParentId.length()-5);
        	 if(parDepartId.startsWith("C")){//表示是所新建的联系人是直属公司下的。
        		 if(new TbDepartDAO().add(new TbDepart(departName,0,departMemo))){
        			 int maxDepartId = new TbDepartDAO().getMaxDepartId();
        			 if(new TbCompanyDepartMiddleDAO().add(new TbCompanyDepartMiddle(Integer.parseInt(lastParentId),maxDepartId))){
        				  request.getSession().setAttribute("result", "添加成功");
        			 }else{
        				  request.getSession().setAttribute("result", "添加失败");
        			 }
        		 }else{
        			 request.getSession().setAttribute("result", "添加失败");
        		 }
             }else if(parDepartId.startsWith("D")){//新建联系人是属于某个组下的、
             	if(new TbDepartDAO().add(new TbDepart(departName,Integer.parseInt(lastParentId),departMemo))){
             		 int maxDepartId = new TbDepartDAO().getMaxDepartId();
             		TbCompanyDepartMiddle tbCompanyDepart = (new TbCompanyDepartMiddleDAO().getCompanyDepartListByDepartId(Integer.parseInt(lastParentId)).get(0));
             		TbCompany tbCompany = new TbCompanyDAO().getTbCompanyByCompanyId(tbCompanyDepart.getCompanyId());
             		//存放此部门所属公司到中间表中
             		if(new TbCompanyDepartMiddleDAO().add(new TbCompanyDepartMiddle(tbCompany.getCompanyId(),maxDepartId))){
             			 request.getSession().setAttribute("result", "添加成功");
             		}else
             			 request.getSession().setAttribute("result", "添加失败");
             	}else request.getSession().setAttribute("result", "添加失败");
             }
        	goList(request, response);
        }
        if("del".equals(method)){
        	 String parDepartId = request.getParameter("delIds");
        	 String lastParentId = parDepartId.substring(1);
        	 lastParentId = lastParentId.substring(0, lastParentId.length()-5);
            if(UtilDAO.delin("Tb_Depart", "DepartId",lastParentId)){
            	if(UtilDAO.delin("Tb_Contact", "DepartId", lastParentId)){
            		if(UtilDAO.delin("Tb_CompanyDepartMiddle", "DepartId", lastParentId)){
            			request.getSession().setAttribute("result", "删除成功");
            		}else
            			request.getSession().setAttribute("result", "删除失败");
            	}else
            		request.getSession().setAttribute("result", "删除失败");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);            
        }
        if("edit".equals(method)){
        String departName=request.getParameter("departName");
        String departMemo=request.getParameter("departMemo");
        String lastParentId = request.getParameter("parDepartId").substring(1);
   	 	lastParentId = lastParentId.substring(0, lastParentId.length()-5);
   	 	int departId=Integer.parseInt(lastParentId);
            if(new TbDepartDAO().edit(new TbDepart(departId,departName,departMemo))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method)) goList(request, response);
        if ("listById".equals(method)) {
        	 String parDepartId = request.getParameter("departId");
        	 String lastParentId = parDepartId.substring(1);
        	 lastParentId = lastParentId.substring(0, lastParentId.length()-5);
             request.getSession(true).setAttribute("tbDepart",new TbDepartDAO().getTbDepartByDepartId(Integer.parseInt(lastParentId)));
             request.getSession().setAttribute("parDepartId", parDepartId);
            response.sendRedirect("ContactModule/TbDepartOperate.jsp");
    	}
        if("showadd".equals(method)){
        	 String parDepartId = request.getParameter("parDepart");
        	 request.getSession().removeAttribute("tbDepart");
        	 request.getSession().setAttribute("parDepartId", parDepartId);
	         response.sendRedirect("ContactModule/TbDepartOperate.jsp");
	    }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.getSession(true).setAttribute("result", new TbContactDAO().getLastResult());
       response.sendRedirect("ContactModule/TbContactList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
}
